#ifndef ALSATOOL_H
#define ALSATOOL_H

#include <QThread>
#include <alsa/asoundlib.h>
#include <QUdpSocket>
#include <QMutex>

#define AUDIO_RATE 16000;
#define PACKETSIZE 1024;
#define AUDIO_POST_CNT 100;

class WriteAudioThread : public QThread
{
    Q_OBJECT
public:
    explicit WriteAudioThread(QObject *parent = nullptr);
    ~WriteAudioThread();

    int set_hwparams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, unsigned int channels, unsigned int rate, snd_pcm_uframes_t &buffer_size,
                     snd_pcm_uframes_t &period_size);

    int  set_swparams(snd_pcm_t *handle, snd_pcm_sw_params_t *swparams, snd_pcm_uframes_t &buffer_size, snd_pcm_uframes_t &period_size);
    int  xrun_recovery(snd_pcm_t *handle, int err);
    void freeHandle(snd_pcm_t *handle0, snd_pcm_t *handle1, snd_pcm_t *handle2, snd_pcm_t *handle3, snd_pcm_t *handle4, snd_pcm_t *handle5);
    void alsaPlaybackVolume(QString element, int value);

    void run();

    void setMulticastGroup(const QHostAddress &groupAddress, quint16 port);
    void destroyMulticastGroup(const QHostAddress &groupAddress);
    void setProcessFlag(bool flag);
    void filterAudio(uint8_t ip);

private:
    snd_pcm_t *handle  = NULL;
    snd_pcm_t *handle1 = NULL;
    snd_pcm_t *handle2 = NULL;
    snd_pcm_t *handle3 = NULL;
    snd_pcm_t *handle4 = NULL;
    snd_pcm_t *handle5 = NULL;

    QUdpSocket m_pRecvManStreamSocket;

    bool    isStart = false;
    uint8_t filter  = 0;

signals:

public slots:
    void dealVolDone(int);
};

#endif  // ALSATOOL_H
